package com.rdk.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rdk.constant.Forward;
import com.rdk.constant.StrConstant;
import com.rdk.service.TShareMessageService;
import com.rdk.utils.BusinessException;
import com.rdk.utils.CommonUtil;
import com.rdk.utils.DateUtil;
import com.rdk.utils.StringUtil;

/**
 * 分享app
 * @author wang_lei
 *
 */
@Controller
public class ShareMessageAction extends BaseAction{
	
	@Autowired
	private TShareMessageService shareMessageService;
	
	/**
	 * 发送消息首页
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "message")
	public String toMessage(HttpServletRequest request, ModelMap map){
		
		return Forward.ActionPage.SHARE_MESSAGE;
	}
	
	/**
	 * 消息
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "message/{offset}")
	public ModelAndView messages(HttpServletRequest request, ModelMap map,@PathVariable int offset) throws BusinessException{
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", 1);
		object.put("resultDes", "");
		String timestamp =DateUtil.getCurrentDateTime();
        if (StringUtil.isEmpty(timestamp)) {
            throw new BusinessException("缺少时间戳！");
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray messages = shareMessageService.findMessages(userId, timestamp, offset);
        jsonObject.put("messages", messages);
        jsonObject.put(StrConstant.TIMESTAMP, timestamp);
        jsonObject.put(StrConstant.OFFSET, offset + StrConstant.PAGE_SIZE20);
        jsonObject.put("next", messages.size() == StrConstant.PAGE_SIZE20);
        object.put("data",jsonObject);
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 消息
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "{shareId}/like")
	public ModelAndView addLike(@PathVariable String shareId,HttpServletRequest request, ModelMap map){
		
		//String userId = CommonUtil.getSession(request, StrConstant.USERID); 
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", "1");
		object.put("resultDes", "");
		if (StringUtil.isEmpty(shareId)) {
            try {
				throw new BusinessException("获取分享失败");
			} catch (BusinessException e) {
				e.printStackTrace();
			}
        }
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
        shareMessageService.addLike(shareId,userId, CommonUtil.getRequest(request, "commentTo"));
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 消息
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "{shareId}/cancelLike")
	public ModelAndView cancelLike(HttpServletRequest request, ModelMap map,@PathVariable String shareId){
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		Map<String,Object> object = new HashMap<String,Object>();
		if (StringUtil.isEmpty(shareId)) {
            try {
				throw new BusinessException("获取分享失败");
			} catch (BusinessException e) {
				e.printStackTrace();
			}
        }
		object.put("resultCode", "1");
		object.put("resultDes", "");
        shareMessageService.cancelLike(shareId, userId);
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 消息
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "{shareId}/comment")
	public ModelAndView comment(HttpServletRequest request, ModelMap map,@PathVariable String shareId)throws BusinessException{
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", 1);
		object.put("resultDes", "");
		if (StringUtil.isEmpty(shareId)) {
            throw new BusinessException("获取分享失败");
        }
		object.put("data", shareMessageService.comment(shareId, request,userId));
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 删除评论
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "comment/{messageId}")
	public ModelAndView deleteComment(HttpServletRequest request, ModelMap map,@PathVariable String messageId) throws BusinessException{
		
		//String userId = CommonUtil.getSession(request, StrConstant.USERID); 
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", "1");
		object.put("resultDes", "");
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		shareMessageService.deleteComment(messageId, userId);
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 获取消息个数
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "{userId}/message/count")
	public ModelAndView messageCount(HttpServletRequest request, ModelMap map,@PathVariable String userId){

		userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", 1);
		object.put("resultDes", "");
		
		object.put("data", shareMessageService.getUnreadMessageCount(userId));
		return new ModelAndView("jsonView", object);
	}
	
}
